#ifndef _THREADPOOL_H
#define _THREADPOOL_H

#include"MutexLock.h"

#include<thread>
#include<functional>
#include<deque>
#include<vector>
#include<pthread.h>
#include<assert.h>

class threadpool
{
public:
    typedef std::function<void()> Task;

    threadpool(int ThreadNums = 2, size_t MaxQuestSize = 200) : 
                                            runing_(false),
                                            MaxQuestSize_(MaxQuestSize),
                                            ThreadNums_(ThreadNums),
                                            mutex_(),
                                            notEmpty_(mutex_),
                                            notFull_(mutex_)
    { }

    ~threadpool()
    {
        runing_ = false;
        for(auto &t : threads_)
            t->join();
    }

    void start();

    void stop() { runing_ = false; }

    void add(Task sk);

    Task task();
    
    void RunInto();

    static void tmp(void* arg);

private:

    std::deque<Task> queue_;
    std::vector<std::unique_ptr<std::thread>> threads_;
    bool runing_;
    size_t MaxQuestSize_;
    size_t ThreadNums_;
    mutable MutexLock mutex_;
    Condition notEmpty_;
    Condition notFull_;

}; // class threadpool end


#endif